package com.owen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.owen.dao.UserDao;
import com.owen.pojo.User;
import com.owen.service.UserService;
import com.owen.util.JxlUtil;
import jxl.write.WriteException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service("baseAreaService")
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public void downLoadExcel(HttpServletResponse response) throws WriteException, IOException, IllegalAccessException {

        String fileName=new String("用户信息.xls".getBytes("GBK"),"ISO8859_1");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=".concat(fileName));
        Map<String,String> map = new LinkedHashMap<String,String>();
        map.put("id", "用户id");
        map.put("name", "学生姓名");
        map.put("age", "学生年龄");
        map.put("address", "学生地址");
        LambdaQueryWrapper<User> userQuery = new LambdaQueryWrapper<User>();
        List<User> users = userDao.selectList(userQuery);

        InputStream is = JxlUtil.getExcelAndData(map,users);
        ServletOutputStream os = response.getOutputStream();
        FileCopyUtils.copy(is, os);
        is.close();
        os.flush();
        os.flush();
    }

}
